//$Id: examplewindow.cc 836 2007-05-09 03:02:38Z jjongsma $ -*- c++ -*-

/* gtkmm example Copyright (C) 2002 gtkmm development team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <iostream>
#include <memory>
#include <gconfmm.h>
#include <glibmm/ustring.h>
#include "examplewindow.h"

using namespace std;

Gtk::Window* ExampleWindow::m_pAbout = 0;	 

ExampleWindow::ExampleWindow()
{
	m_client = Gnome::Conf::Client::get_default_client();
	m_client->add_dir("/apps/hellotechie/hexedit",
		Gnome::Conf::CLIENT_PRELOAD_ONELEVEL);

	const string dir_path = Glib::get_user_data_dir();	 
	set_icon_from_file(dir_path + 
		"/Hexedit/pixmaps/ethane-sm.png");
	 
	set_title(APP_NAME);
	set_border_width(10);
	set_default_size(450, 400);

	set_position(Gtk::WIN_POS_CENTER);	 
	m_bAboutShown = false;
	m_bToolbar = true;
	 
	m_refActionGroup = Gtk::ActionGroup::create();
	m_refActionGroup->add(Gtk::Action::create("FileMenu", "_File"));
	m_refActionGroup->add(Gtk::Action::create("FileOpen",
		Gtk::Stock::OPEN, "_Open", "Open an existing document"),
      sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	 
	m_refActionGroup->add(Gtk::Action::create("FileNew", Gtk::Stock::NEW, "_New", "Create a new document"),
          sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new_generic));
	m_refActionGroup->add(Gtk::Action::create("FileQuit", Gtk::Stock::QUIT, "_Quit", "Exit the application"),
						  sigc::mem_fun(*this, &ExampleWindow::on_menu_file_quit));
	//Edit menu:
	m_refActionGroup->add(Gtk::Action::create("EditMenu", "_Edit"));
	m_refActionGroup->add(Gtk::Action::create("UndoEdit", Gtk::Stock::UNDO, "_Undo last action", "Undoes the last action"),
		  sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("RedoEdit", Gtk::Stock::REDO, "_Redo last action", "Redo the last action"),
		  sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	 
	m_refActionGroup->add(Gtk::Action::create("EditCopy", Gtk::Stock::COPY, "_Copy", "Copy data into edit buffer"),
          sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("EditPaste", Gtk::Stock::PASTE, "_Paste", "Paste data from edit buffer to point"),
          sigc::mem_fun(*this, &ExampleWindow::on_menu_others));

	m_refActionGroup->add(Gtk::Action::create("DiskMenu", "_Disk"));
	m_refActionGroup->add(Gtk::Action::create("DiskOpen", Gtk::Stock::HARDDISK, "_Open Filesystem...", "Open a file system for sector editing"),
          sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("DiskCopy", Gtk::Stock::COPY, "_Copy Sectors...", "Copy sectors between file systems"),
          sigc::mem_fun(*this, &ExampleWindow::on_menu_others));

	m_refActionGroup->add(Gtk::Action::create("OptionsMenu", "_Options"));
	m_refActionGroup->add(Gtk::Action::create("ByteOrderMenu", "_Byte Order"));
	Gtk::RadioAction::Group group_userlevel;

	m_refChoiceOne = Gtk::RadioAction::create(group_userlevel, "LittleEndian", "Little Endian", "Displays the byte order of words in Intel format");
	m_refActionGroup->add(m_refChoiceOne,
						  sigc::mem_fun(*this, &ExampleWindow::on_menu_choices_one));
	m_refChoiceTwo = Gtk::RadioAction::create(group_userlevel, "BigEndian", "Big Endian", "Displays the byte order of words in Motorola format");
	m_refActionGroup->add(m_refChoiceTwo,
          sigc::mem_fun(*this, &ExampleWindow::on_menu_choices_two));
	
	m_refActionGroup->add(Gtk::Action::create("FileOffsetMenu", "_File Offset"));
	Gtk::RadioAction::Group group_offset;
	m_refOffsetHex = Gtk::RadioAction::create(group_offset, "FileOffsetHex", "Hex", "Display offsets in hexidecimal format");
	m_refActionGroup->add(m_refOffsetHex,
						  sigc::mem_fun(*this, &ExampleWindow::on_menu_choices_one));

	m_refOffsetDec = Gtk::RadioAction::create(group_offset, "FileOffsetDec", "Decimal", "Display offsets in decimal format");
	m_refActionGroup->add(m_refOffsetDec,
						  sigc::mem_fun(*this, &ExampleWindow::on_menu_choices_two));

	string str(DATA_DIR);
	str.append("/window/toolbar");
	bool bTbar = m_client->get_bool(str);
	 
	m_refActionGroup->add(Gtk::ToggleAction::create("ShowToolbar", "Show Toolbar", "Toggles display of the toolbar", bTbar),
		sigc::mem_fun(*this, &ExampleWindow::on_toggle_toolbar));
	 
	m_refToolbar = Glib::RefPtr<Gtk::ToggleAction>::cast_static(m_refActionGroup->get_action("ShowToolbar"));

	string str2(DATA_DIR);
	str2.append("/window/inspect");
	bool bInspect = m_client->get_bool(str2);
	 
	m_refActionGroup->add(Gtk::ToggleAction::create("ShowInspector", "Data Inspector", "Toggles data inspector view", bInspect),
		sigc::mem_fun(*this, &ExampleWindow::on_toggle_datains));
	 
	//m_refToolbar = Glib::RefPtr<Gtk::ToggleAction>::cast_static(m_refActionGroup->get_action("ShowInspector"));

	string str3(DATA_DIR);
	str3.append("/window/results");
	bool bResults = m_client->get_bool(str3);
	 
	m_refActionGroup->add(Gtk::ToggleAction::create("ShowResults", "Results Window", "Toggles results window view", bResults),
		sigc::mem_fun(*this, &ExampleWindow::on_toggle_results));
	 
	//m_refToolbar = Glib::RefPtr<Gtk::ToggleAction>::cast_static(m_refActionGroup->get_action("ShowResults"));
	 
	m_refActionGroup->add(Gtk::Action::create("OptOptions", Gtk::Stock::PREFERENCES, "_Options", "Presents the options dialog"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));

	m_refActionGroup->add(Gtk::Action::create("ToolsMenu", "_Tools"));
	m_refActionGroup->add(Gtk::Action::create("ToolsOps", "_Operations"));
	m_refActionGroup->add(Gtk::Action::create("flip", Gtk::Stock::PREFERENCES, "Byte Flip...", "Byte Flip"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("inv", Gtk::Stock::PREFERENCES, "Inverse Bits", "Inverse Bits"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("shleft", Gtk::Stock::PREFERENCES, "Shift Left...", "Shift Left"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("shright", Gtk::Stock::PREFERENCES, "Shift Right...", "Shift Right"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("rotleft", Gtk::Stock::PREFERENCES, "Rotate Left...", "Rotate Left"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others)); 
	m_refActionGroup->add(Gtk::Action::create("rotright", Gtk::Stock::PREFERENCES, "Rotate Right...", "Rotate Right"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("blleft", Gtk::Stock::PREFERENCES, "Block Shift Left...", "Block Shift Left"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("blright", Gtk::Stock::PREFERENCES, "Block Shift Right...", "Block Shift Right"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("setceil", Gtk::Stock::PREFERENCES, "Set Ceiling Value...", "Set Ceiling Value"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("setfloor", Gtk::Stock::PREFERENCES, "Set Floor Value...", "Set Floor Value"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("and", Gtk::Stock::PREFERENCES, "AND...", "AND"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("or", Gtk::Stock::PREFERENCES, "OR...", "OR"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others)); 
	m_refActionGroup->add(Gtk::Action::create("xor", Gtk::Stock::PREFERENCES, "XOR...", "XOR"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));	 
	m_refActionGroup->add(Gtk::Action::create("change", Gtk::Stock::PREFERENCES, "Change sign...", "Change Sign"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others)); 
	m_refActionGroup->add(Gtk::Action::create("add", Gtk::Stock::PREFERENCES, "Add...", "Add"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("sub", Gtk::Stock::PREFERENCES, "Subtract...", "Subtract"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("mul", Gtk::Stock::PREFERENCES, "Multiply...", "Multiply"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("div", Gtk::Stock::PREFERENCES, "Divide...", "Divide"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("upper", Gtk::Stock::PREFERENCES, "Upper Case", "Upper Case"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("lower", Gtk::Stock::PREFERENCES, "Lower Case", "Lower Case"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("invcse", Gtk::Stock::PREFERENCES, "Inverse Case", "Inverse Case"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("ToolsMrks", "_Bookmarks"));
	m_refActionGroup->add(Gtk::Action::create("addmrk", Gtk::Stock::PREFERENCES, "_Add Bookmark", "Add a new bookmark"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("nextmrk", Gtk::Stock::PREFERENCES, "_Next Bookmark", "Move to next bookmark"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("prevmrk", Gtk::Stock::PREFERENCES, "_Previous Bookmark", "Move to previous bookmark"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("gotomrk", Gtk::Stock::PREFERENCES, "_Goto Selected Bookmark", "Set the cursor position to the start of this bookmark"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("seldata", Gtk::Stock::PREFERENCES, "_Select Bookmark Data", "Select data specified by this bookmark"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("editmrk", Gtk::Stock::PREFERENCES, "_Edit Bookmark", "Edit an existing bookmark"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("remmrk", Gtk::Stock::PREFERENCES, "_Remove Bookmark", "Remove the selected bookmark"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("opencoll", Gtk::Stock::PREFERENCES, "_Open Bookmark Collections", "Open bookmark collection"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("save", Gtk::Stock::PREFERENCES, "_Save", "Save bookmarks to file"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
  	m_refActionGroup->add(Gtk::Action::create("saveas", Gtk::Stock::PREFERENCES, "Save _As...", "Save current bookmarkk collection to new file"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
  	m_refActionGroup->add(Gtk::Action::create("clrmrks", Gtk::Stock::PREFERENCES, "C_lear Bookmarks", "Clear all bookmarks"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
  	m_refActionGroup->add(Gtk::Action::create("colprops", Gtk::Stock::PREFERENCES, "Collection _Properties...", "Displays properties for the bookmark collection"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("distrib", Gtk::Stock::PREFERENCES, "Character _Distribution", "View the distribution of characters"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("comp", "Com_pare"));
 	m_refActionGroup->add(Gtk::Action::create("files", Gtk::Stock::PREFERENCES, "_Compare Files", "Load the compare tool"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));	 
	m_refActionGroup->add(Gtk::Action::create("recomp", Gtk::Stock::PREFERENCES, "_Recompare", "Recmpare files"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
  	m_refActionGroup->add(Gtk::Action::create("clrres", Gtk::Stock::PREFERENCES, "C_lear Results", "Clear the previous compare results"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
  	m_refActionGroup->add(Gtk::Action::create("next", Gtk::Stock::PREFERENCES, "_Next Difference", "Displays the next comparison difference"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
  	m_refActionGroup->add(Gtk::Action::create("prev", Gtk::Stock::PREFERENCES, "_Previous Difference", "Displays the previous comparison difference"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
  	m_refActionGroup->add(Gtk::Action::create("Generate", Gtk::Stock::PREFERENCES, "_Generate Checksum...", "Generate a checksum on the file in the current view"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
  	m_refActionGroup->add(Gtk::Action::create("FindStr", Gtk::Stock::PREFERENCES, "_Find Strings...", "Searches the document for all ascii and unicode strings"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
  	m_refActionGroup->add(Gtk::Action::create("StructVwer", "Structure _Viewer"));
	m_refActionGroup->add(Gtk::Action::create("selstruct", "_Select Structure Library", "Select a library of structures"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("clselib", "C_lose Library", "Close the working structure library"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("addstruct", "_Add Structure", "Add a structure from the working structure library"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
 	m_refActionGroup->add(Gtk::Action::create("remstruct", "_Remove Structure", "Remove the selected structure"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
 	m_refActionGroup->add(Gtk::Action::create("clrstruct", "_Clear Structures", "Clear all structures"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
 	m_refActionGroup->add(Gtk::Action::create("lockstruct", "L_ock Structure", "Lock structure to document offset"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("fltstruct", "_Float Structure", "Float structure across documents/offsets"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("gotoaddr", "_Goto Address", "Go to the address of the selected "),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("HelpMenu", "_Help", "Displays the help file"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add(Gtk::Action::create("AppHelp", Gtk::Stock::HELP, "Contents", "Displays the application documentation"),
		sigc::mem_fun(*this, &ExampleWindow::on_menu_others));
	m_refActionGroup->add( Gtk::Action::create("HelpAbout", Gtk::Stock::ABOUT, "About...", "Application credits"),
		sigc::mem_fun(*this, &ExampleWindow::on_app_about));

	m_refUIManager = Gtk::UIManager::create();
	m_refUIManager->insert_action_group(m_refActionGroup);

	add_accel_group(m_refUIManager->get_accel_group());

	m_refUIManager->signal_connect_proxy().
		connect(sigc::mem_fun(*this,
							  &ExampleWindow::setup_statusbar_tooltip));

	Glib::ustring ui_info = 
		"<ui>"
		"  <menubar name='MenuBar'>"
		"    <menu action='FileMenu'>"
		"	   <menuitem action='FileNew'/>"
		"	   <menuitem action='FileOpen'/>"
		"      <separator/>"
		"      <menuitem action='FileQuit'/>"
		"    </menu>"
		"    <menu action='EditMenu'>"
		"      <menuitem action='UndoEdit'/>"
		"      <menuitem action='RedoEdit'/>"
		"      <separator/>"
		"      <menuitem action='EditCopy'/>"
		"      <menuitem action='EditPaste'/>"
		"    </menu>"
		"    <menu action='DiskMenu'>"
		"      <menuitem action='DiskOpen'/>"
		"	   <separator/>"
		"      <menuitem action='DiskCopy'/>"
		"    </menu>"
		"    <menu action='OptionsMenu'>"
		"	     <menu action='ByteOrderMenu'>"
		"          <menuitem action='LittleEndian'/>"
		"          <menuitem action='BigEndian'/>"
		"       </menu>"
		"       <menu action='FileOffsetMenu'>"
		"          <menuitem action='FileOffsetHex'/>"
		"          <menuitem action='FileOffsetDec'/>"
		"       </menu>"
		"       <separator/>"
		"	     <menuitem action='ShowToolbar'/>"
		"		  <menuitem action='ShowInspector'/>"
		"		  <menuitem action='ShowResults'/>"
		"       <menuitem action='OptOptions'/>"
		"    </menu>"
		"    <menu action='ToolsMenu'>"
		"       <menu action='ToolsOps'>"
		"          <menuitem action='flip'/>"
		"          <menuitem action='inv'/>"
		"          <menuitem action='shleft'/>"
		"          <menuitem action='shright'/>"
		"          <menuitem action='rotleft'/>"
		"          <menuitem action='rotright'/>"
		"          <menuitem action='blleft'/>"
		"          <menuitem action='blright'/>"
		"          <menuitem action='setceil'/>"
		"          <menuitem action='setfloor'/>"
		"          <separator/>"
		"          <menuitem action='and'/>"
		"          <menuitem action='or'/>"
		"          <menuitem action='xor'/>"
		"          <separator/>"
		"          <menuitem action='change'/>"
		"          <menuitem action='add'/>"
		"          <menuitem action='sub'/>"
		"          <menuitem action='mul'/>"
		"          <menuitem action='div'/>"
		"          <menuitem action='mod'/>"
		"          <separator/>"
		"          <menuitem action='upper'/>"
		"          <menuitem action='lower'/>"
		"          <menuitem action='invcse'/>"
		"       </menu>"
		"       <separator/>"
		"       <menu action='ToolsMrks'>"
		"          <menuitem action='addmrk'/>"
		"          <menuitem action='nextmrk'/>"
		"          <menuitem action='prevmrk'/>"
		"          <separator/>"
		"          <menuitem action='gotomrk'/>"
		"          <menuitem action='seldata'/>"
		"          <menuitem action='editmrk'/>"
		"          <menuitem action='remmrk'/>"
		"          <separator/>"
		"          <menuitem action='opencoll'/>"
		"          <menuitem action='save'/>"
		"          <menuitem action='saveas'/>"
		"          <separator/>"
		"          <menuitem action='clrmrks'/>"
		"          <menuitem action='colprops'/>"
		"       </menu>"
		"       <menuitem action='distrib'/>"
		"       <menu action='comp'>"
		"          <menuitem action='files'/>"
		"          <menuitem action='recomp'/>"
		"          <menuitem action='clrres'/>"
		"          <separator/>"
		"          <menuitem action='next'/>"
		"          <menuitem action='prev'/>"
		"       </menu>"
		"       <menuitem action='Generate'/>"
		"       <menuitem action='FindStr'/>"
		"       <menu action='StructVwer'>"
		"          <menuitem action='selstruct'/>"
		"          <menuitem action='clselib'/>"
		"          <separator/>"
		"          <menuitem action='addstruct'/>"
		"          <menuitem action='remstruct'/>"
		"          <menuitem action='clrstruct'/>"
		"          <separator/>"
		"          <menuitem action='lockstruct'/>"
		"          <menuitem action='fltstruct'/>"
		"          <separator/>"
		"          <menuitem action='gotoaddr'/>"
		"       </menu>"
		"    </menu>"
		"    <menu action='HelpMenu'>"
		"      <menuitem action='AppHelp'/>"
		"      <menuitem action='HelpAbout'/>"
		"    </menu>"
		"  </menubar>"
		"  <toolbar name='ToolBar'>"
 		"    <toolitem action='FileNew'/>"
		"    <toolitem action='FileQuit'/>"
		"    <toolitem action='EditCopy'/>"
		"    <toolitem action='EditPaste'/>"
		"    <toolitem action='OptOptions'/>"
		"  </toolbar>"
		"</ui>";

#ifdef GLIBMM_EXCEPTIONS_ENABLED
	try
	{
		m_refUIManager->add_ui_from_string(ui_info);
	}
	catch(const Glib::Error& ex)
	{
		std::cerr << "building menus failed: " <<  ex.what();
	}
#else
	auto_ptr<Glib::Error> ex;
	m_refUIManager->add_ui_from_string(ui_info, ex);
	
	if(ex.get())
		std::cerr << "building menus failed: " <<  ex->what();
#endif

	Gtk::Widget* pMenubar = m_refUIManager->get_widget("/MenuBar");
	if(pMenubar)
	    m_Box.pack_start(*pMenubar, Gtk::PACK_SHRINK);

	Gtk::Widget* pToolbar = m_refUIManager->get_widget("/ToolBar");
	if(pToolbar)
   	m_Box.pack_start(*pToolbar, Gtk::PACK_SHRINK);

	add(m_Box);
	m_Notebook.set_tab_pos(Gtk::POS_BOTTOM);
	//m_Notebook.pages().push_back(Gtk::Notebook_Helpers::TabElem(m_MessagesList,
		//"Messages"));
	 
	//m_Notebook.append_page(m_Label1, "Page one");
	m_VPaned.add1(m_Notebook);
	m_VPaned.add2(m_HPaned);
	m_VPaned.set_border_width(10);

	m_HPaned.add1(m_MessageText);
	m_HPaned.add2(m_MessageText3);
	m_Box.pack_start(m_VPaned);
	 
	m_Box.pack_start(m_HPaned);
	m_Box.pack_start(m_statusbar, Gtk::PACK_SHRINK);
	get_paned_pos();

	show_all_children();
	 
	(bTbar)? pToolbar->show() : pToolbar->hide();
	(bInspect)? m_MessageText.show() : m_MessageText.hide();
	(bResults)? m_MessageText3.show() : m_MessageText3.hide();
}

ExampleWindow::~ExampleWindow()
{
}


void ExampleWindow::on_menu_file_quit()
{
	 set_paned_pos();
    hide();
}

void ExampleWindow::on_menu_file_new_generic()
{
   std::cout << "A File|New menu item was selected." << std::endl;
}

void ExampleWindow::on_menu_others()
{
  std::cout << "A menu item was selected." << std::endl;
}

void ExampleWindow::on_menu_choices_one()
{
  Glib::ustring message;
  if(m_refChoiceOne->get_active())
    message = "Choice 1 was selected.";
  else
    message = "Choice 1 was deselected";

  std::cout << message << std::endl;
}

void ExampleWindow::on_menu_choices_two()
{
	Glib::ustring message;
	if(m_refChoiceTwo->get_active())
		message = "Choice 2 was selected.";
	else
		message = "Choice 2 was deselected";

	std::cout << message << std::endl;
}

void
ExampleWindow::setup_statusbar_tooltip(const Glib::RefPtr<Gtk::Action> &action, Gtk::Widget *widget)
{
    Gtk::Item *item = dynamic_cast<Gtk::Item*> (widget);
    if(item)
    {
        item->signal_select().connect(
        	  sigc::bind(sigc::mem_fun(*this,
	        &ExampleWindow::menuitem_focus_cb), action));
		  
        item->signal_deselect().connect(
        	  sigc::bind(sigc::mem_fun(m_statusbar, &Gtk::Statusbar::pop),
	        guint(STATUSBAR_MENU_TIP)));
    }
}

void
ExampleWindow::menuitem_focus_cb(const Glib::RefPtr<Gtk::Action> &action)
{
    m_statusbar.push(action->property_tooltip(), STATUSBAR_MENU_TIP);
}

void
ExampleWindow::on_app_about()
{
	if(m_pAbout && m_bAboutShown)
	{
		m_pAbout->set_transient_for(*this);
		Glib::RefPtr<Gdk::Window> about_win = m_pAbout->get_window();
		about_win->show();
		about_win->raise();
	}
	else
	{
		if(m_pAbout)
		{
			delete m_pAbout;
			m_pAbout = 0;
		}

		Gtk::AboutDialog* pDerived = new Gtk::AboutDialog;
		m_pAbout = pDerived;
		const string dir_path = Glib::get_user_data_dir();
		
		pDerived->set_icon_from_file(dir_path +
		    "/Hexedit/pixmaps/ethane-sm.png");
		pDerived->set_name(APP_NAME);
		
		pDerived->set_version(APP_VER);
		pDerived->set_logo(Gdk::Pixbuf::create_from_file(dir_path +
		    "/Hexedit/pixmaps/ethane-m.png"));
		    
		pDerived->set_copyright("Copyright © 2010 William Ross");
		pDerived->set_comments("Based on Hex Workshop by BreakPoint Software");
		pDerived->set_url_hook(sigc::mem_fun(*this,
		    &ExampleWindow::about_uri_cb));
		    
		pDerived->set_website("http://www.bpsoft.com/");
		pDerived->set_website_label("BreakPoint Software Website");
		m_vAuth.push_back("William Ross <bill.aboutus@gmail.com>");
		
		pDerived->set_authors(m_vAuth);
		pDerived->set_documenters(m_vAuth);
		m_pAbout->signal_hide().connect(sigc::mem_fun(*this,
		    &ExampleWindow::on_about_close));
		    
		m_bAboutShown = true;
		static_cast<Gtk::Dialog*>(m_pAbout)->run();
		m_pAbout->hide();
	}
}

void
ExampleWindow::about_uri_cb(Gtk::AboutDialog &about, const Glib::ustring &link)
{
	GError* gerror = 0;
	gtk_show_uri(0, "http://www.bpsoft.com/", GDK_CURRENT_TIME, &gerror);
}

void
ExampleWindow::on_about_close()
{
	m_bAboutShown = false;
}

void
ExampleWindow::on_toggle_toolbar()
{	
	Gtk::Widget* pToolbar = m_refUIManager->get_widget("/ToolBar");
	string str(DATA_DIR);
	str.append("/window/toolbar");
	 
	if(pToolbar->get_visible())
	{
		pToolbar->hide();
		m_client->set(str, false);
	}
	else
	{
		pToolbar->show();
		m_client->set(str, true);
	}
}

void
ExampleWindow::on_toggle_datains()
{
	string str(DATA_DIR);
	str.append("/window/inspect");
	 
	if(m_MessageText.get_visible())
	{
		m_MessageText.hide();
		m_client->set(str, false);
	}
	else
	{
		m_MessageText.show();
		m_client->set(str, true);
	}
	hide_hpane();
}

void
ExampleWindow::on_toggle_results()
{
	string str(DATA_DIR);
	str.append("/window/results");
	 
	if(m_MessageText3.get_visible())
	{
		m_MessageText3.hide();
		m_client->set(str , false);
	}
	else
	{
		m_MessageText3.show();
		m_client->set(str, true);
	}	 
	hide_hpane();
}

void
ExampleWindow::get_paned_pos()
{
	string strV(DATA_DIR);
	strV.append("/window/main_v");
	 
	int nPosVert = m_client->get_int(strV);
	m_VPaned.set_position(nPosVert);

	string strH(DATA_DIR);
	strH.append("/window/main_h");
	int nPosHorz = m_client->get_int(strH);
	m_HPaned.set_border_width(1);
	m_HPaned.set_position(nPosHorz);
}

void
ExampleWindow::set_paned_pos()
{
	int nPosVert = m_VPaned.get_position();
	string strV(DATA_DIR);
	strV.append("/window/main_v");
	 
	m_client->set(strV, nPosVert);
	string strH(DATA_DIR);
	strH.append("/window/main_h");
	 
	int nPosHorz = m_HPaned.get_position();
	m_client->set(strH , nPosHorz);
}

void
ExampleWindow::hide_hpane()
{
	if(!m_MessageText3.get_visible() && !m_MessageText.get_visible())
		m_HPaned.hide();
	else if(m_MessageText3.get_visible() || m_MessageText.get_visible())
		m_HPaned.show();
}
